package com.alks.function.service.serviceImpl.finance.fincons;

import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelPoiUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.PcMtlCheck2;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.finance.fincons.InboundDetailsDto;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherInRequest;
import com.alks.function.mapper.PcSlipDetailSoleMapper;
import com.alks.function.mapper.finance.fincons.ReceiptReconciliationMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcMtlCheckMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSlipDetailPackMapper;
import com.alks.function.service.finance.fincons.ReceiptReconciliationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReceiptReconciliationServiceImpl implements ReceiptReconciliationService {

    @Autowired
    private ReceiptReconciliationMapper receiptReconciliationMapper;

    @Autowired
    private SD_SQ_PcMtlCheckMapper checkMapper;

    @Autowired
    private SD_SQ_PcSlipDetailPackMapper detailPackMapper;

    @Autowired
    private PcSlipDetailSoleMapper pcSlipDetailSoleMapper;

    /**
     * 入库明细查询
     * @param request
     * @return
     */
//    @Override
//    @AutoPageAop
//    public ResponseInfo getInboundDetailsList(InboundDetailsRequest request) {
//        String companyId = UserIdThread.get().getCompanyId();
//        List<InboundDetailsDto> inboundDetailsList = receiptReconciliationMapper.getInboundDetailsList(companyId,request);
//
//        PageInfo list = new PageInfo(inboundDetailsList);
//        HashMap map = new HashMap<>();
//        map.put("inboundDetailsList", inboundDetailsList);
//        map.put("total", list.getTotal());
//        return ResponseInfo.ok(map);
//    }

    /**
     * 导出明细
     * @param request
     * @param response
     */
    @Override
    public void exportInboundDetails(MtlPreOtherInRequest request, HttpServletResponse response) throws IOException {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<InboundDetailsDto> list = checkMapper.mtlPreOtherInQuery(request, companyId);

        /*获取求和*/
        Map<String, BigDecimal> sum = checkMapper.mtlPreOtherSumQuery(request, companyId);
        DecimalFormat format = new DecimalFormat("#.#");

        /*封装输出*/
        Map<String, Object> map = new HashMap<>();
        map.put("qtySum", format.format(sum.get("sendQtySum")));
        map.put("amt", format.format(sum.get("amtSum")));
        map.put("list", list);

//        ExcelUtils.creatByTemplate(response,"入库材料明细模板.xlsx","入库材料明细_"+ DateUtils.dateTimeNow(),list,map);

        //shiqian TODO  :暂时最多存  65535条数据
        String fileName = "入库材料明细_"+ DateUtils.dateTimeNow() +".xls";

        try{
            ExcelUtils.setResponseZF(response, fileName);
            String templatePath = "templates/材料明细模板1.xls";
            ExcelPoiUtils.writeExcelList(response, map, templatePath, fileName);
        } catch (Exception e) {
            log.error("入材料明细明细导出失败, 错误信息: {}", e.getMessage());
        }
    }

    /**
     * 查询当日审核单据数量
     */
    @Override
    public ResponseInfo queryDailyBill() {
        //面材
        Integer count1 = checkMapper.getPcMtlCheckCfmCount(UserIdThread.get().getCompanyId());
        //包材
        Integer count3 = detailPackMapper.getSlipDetailPackCfmCount(UserIdThread.get().getCompanyId());
        //面衬 + 大底
        Integer count4 = pcSlipDetailSoleMapper.getSoleCheckDetailCfmCount(UserIdThread.get().getCompanyId(),"面衬");
        Integer count2 = pcSlipDetailSoleMapper.getSoleCheckDetailCfmCount(UserIdThread.get().getCompanyId(),"大底");

        Map<String, Object> map = new HashMap<>();
        map.put("mianCaiCount",count1);
        map.put("daDiCount",count2);
        map.put("baoCaiCount",count3);
        map.put("mianChenCount",count4);
        return ResponseInfo.ok(map);
    }
}
